import tkinter as tk
from tkinter import messagebox
from login import LoginDialog
from customer_management import CustomerManagement
from employee_management import EmployeeManagement
from good_management import GoodManagement
from purchase_management import PurchaseManagement
from statistics_query import StatisticsQuery

class SuperMarketManagementApp:
    def __init__(self, root):
        self.root = root
        self.root.title("超市管理系统")
        self.root.geometry("1200x900")  # Increased window size
        self.root.resizable(True, True)  # Make the window resizable
        self.create_login_dialog()
        self.root.protocol("WM_DELETE_WINDOW", self.on_closing)  # Handle window close event

    def create_login_dialog(self):
        self.login_dialog = LoginDialog(self.root, self.on_login_success)

    def on_login_success(self):
        self.login_dialog.dialog.destroy()
        self.create_main_menu()

    def create_main_menu(self):
        self.menu_frame = tk.Frame(self.root)
        self.menu_frame.pack(side=tk.LEFT, fill=tk.Y)

        self.content_frame = tk.Frame(self.root)
        self.content_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)

        self.create_menu_buttons()

    def create_menu_buttons(self):
        buttons = [
            ("客户管理", self.open_customer_management),
            ("员工管理", self.open_employee_management),
            ("商品管理", self.open_good_management),
            ("采购管理", self.open_purchase_management),
            ("统计与查询", self.open_statistics_query)
        ]

        for text, command in buttons:
            button = tk.Button(self.menu_frame, text=text, command=command, width=20, height=2)
            button.pack(pady=10)

    def open_customer_management(self):
        self.clear_content_frame()
        CustomerManagement(self.content_frame)

    def open_employee_management(self):
        self.clear_content_frame()
        EmployeeManagement(self.content_frame)

    def open_good_management(self):
        self.clear_content_frame()
        GoodManagement(self.content_frame)

    def open_purchase_management(self):
        self.clear_content_frame()
        PurchaseManagement(self.content_frame)

    def open_statistics_query(self):
        self.clear_content_frame()
        StatisticsQuery(self.content_frame)

    def clear_content_frame(self):
        for widget in self.content_frame.winfo_children():
            widget.destroy()

    def on_closing(self):
        if messagebox.askokcancel("Quit", "Do you want to quit?"):
            self.root.quit()  # Use quit() instead of destroy() to ensure the main loop exits
            self.root.destroy()  # Destroy the root window

if __name__ == "__main__":
    root = tk.Tk()
    app = SuperMarketManagementApp(root)
    root.mainloop()
